package com.ios.service.impl;

import com.ios.dao.DisablerUserMapper;
import com.ios.dao.LoginRecordMapper;
import com.ios.dto.DisablerExecution;
import com.ios.entity.DisablerUser;
import com.ios.entity.LoginRecord;
import com.ios.enums.DisablerStateEnum;
import com.ios.exceptions.DisableOperationException;
import com.ios.service.IDisablerService;
import com.ios.util.ImageUtil;
import com.ios.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * Created by LiuZhe
 * Date : 18-5-10
 * Time : 下午10:10
 */
@Service
public class DisablerServiceImpl implements IDisablerService {

    @Autowired
    private DisablerUserMapper disablerUserMapper;
    @Autowired
    private LoginRecordMapper loginRecordMapper;


    @Override
    public DisablerExecution RegisterDisablerUser(String username, String password_1, String password_2) {
        DisablerUser disablerUser = new DisablerUser();
        if(username.equals(null)||password_1.equals(null)||password_2.equals(null)){
            return new DisablerExecution(DisablerStateEnum.PARAM_NULL);
        }else {
            try {
                if (checkUsername(username)){
                    return new DisablerExecution(DisablerStateEnum.REPEAT_USERNAME);
                }else if (!password_1.equals(password_2)){
                    return new DisablerExecution(DisablerStateEnum.PASSWORD_ERROR);
                }else {
                    disablerUser.setUsername(username);
                    disablerUser.setPassword(password_1);
                    disablerUserMapper.insertSelective(disablerUser);
                    return new DisablerExecution(DisablerStateEnum.SUCCESS,disablerUser);
                }
            }catch (Exception e){
                return new DisablerExecution(DisablerStateEnum.FAIL);
            }
        }
    }

    @Override
    public DisablerExecution CompleteRegisterDisablerUser(DisablerUser disablerUser, InputStream HeadImgInputStream, String fileName) {
        /*判断传入残疾人用户信息是否为空*/
        if (disablerUser == null) {
            return new DisablerExecution(DisablerStateEnum.PARAM_NULL);
        } else if (checkPhone(disablerUser)) {
            /*判断用户手机号是否重复*/
            return new DisablerExecution(DisablerStateEnum.REPEAT_PHONE);
        } else {
            /*给用户注册时间信息赋值*/
            disablerUser.setCreateTime(new Date());
            disablerUser.setUpdateTime(new Date());
            disablerUserMapper.updateByPrimaryKey(disablerUser);
            if (HeadImgInputStream != null) {/*存储图片*/
                try {
                    disablerUser.setHeadImage(addDisablerHeadImg(disablerUser, HeadImgInputStream, fileName));
                    disablerUserMapper.updateHeadImg(addDisablerHeadImg(disablerUser, HeadImgInputStream, fileName), disablerUser.getUsername());
                } catch (Exception e) {
                    throw new DisableOperationException("register error:" + e.getMessage());
                }
            }
            return new DisablerExecution(DisablerStateEnum.SUCCESS, disablerUser);
        }
    }

    @Override
    public DisablerExecution LoginDisablerUser(String username, String password) {
        if (username.equals(null)||password.equals(null)){
            return new DisablerExecution(DisablerStateEnum.PARAM_NULL);
        }else {
            try {
                LoginRecord loginRecord = new LoginRecord();
                loginRecord.setUsername(username);
                loginRecord.setPassword(password);
                loginRecord.setStatus(1);
                loginRecord.setLoginTime(new Date());
                loginRecord.setResult("待验证");
                loginRecordMapper.insert(loginRecord);
                return new DisablerExecution(DisablerStateEnum.SUCCESS,loginRecord);
            }catch (Exception e){
                return new DisablerExecution(DisablerStateEnum.FAIL);
            }
        }
    }

    @Override
    public DisablerExecution CheckLoginDisablerUser(int id) {
        if (id==-1){
            return new DisablerExecution(DisablerStateEnum.PARAM_NULL);
        }else {
            LoginRecord loginRecord = loginRecordMapper.selectbyId(id);
            if (!checkUsername(loginRecord.getUsername())){
                loginRecord.setResult("登录失败");
                loginRecordMapper.update(loginRecord);
                return new DisablerExecution(DisablerStateEnum.ERROR_USERNAME);
            }else {
                DisablerUser disablerUser = disablerUserMapper.selectByUsername(loginRecord.getUsername());
                if (disablerUser.getPassword().equals(loginRecord.getPassword())){
                    loginRecord.setResult("登录成功");
                    loginRecordMapper.update(loginRecord);
                    return new DisablerExecution(DisablerStateEnum.SUCCESS, disablerUser);
                }else {
                    loginRecord.setResult("登录失败");
                    loginRecordMapper.update(loginRecord);
                    return new DisablerExecution(DisablerStateEnum.ERROR_PASSWORD);
                }
            }
        }
    }

    @Override
    public DisablerExecution QueryDisablerUser(String username) {
        DisablerUser disablerUser = null;
        if (!checkUsername(username)){
            /*用户名不存在*/
            return new DisablerExecution(DisablerStateEnum.ERROR_USERNAME);
        }else {
            /*用户存在*/
            try{
                disablerUser = disablerUserMapper.queryByUsername(username);
                return new DisablerExecution(DisablerStateEnum.SUCCESS, disablerUser);
            }catch (Exception e){
                return new DisablerExecution(DisablerStateEnum.INNER_ERROR);
            }
        }
    }

    @Override
    public DisablerExecution EditDisablerUserInfo(DisablerUser disablerUser , InputStream HeadImgInputStream, String fileName) {
        if (checkPhone(disablerUser)) {
            /*判断新用户手机号是否重复*/
            return new DisablerExecution(DisablerStateEnum.REPEAT_PHONE);
        } else if (checkUsername(disablerUser)) {
            /*判断新用户名是否重复*/
            return new DisablerExecution(DisablerStateEnum.REPEAT_USERNAME);
        } else {
            try {
                /*判断图片*/
                if (HeadImgInputStream != null){
                    /*if (disablerUserMapper.selectByPrimaryKey(id).getHeadImage()!=null){
                        *//*原来有图片则删除*//*
                        ImageUtil.deleteFile(disablerUser.getHeadImage());
                        disablerUserMapper.updateHeadImg(null,disablerUser.getUsername());
                    }*/
                    /*添加新图片*/
                    disablerUser.setHeadImage(addDisablerHeadImg(disablerUser, HeadImgInputStream, fileName));
                    disablerUserMapper.updateHeadImg(addDisablerHeadImg(disablerUser, HeadImgInputStream, fileName), disablerUser.getUsername());
                }
                /*给用户更新信息时间信息赋值*/
                disablerUser.setUpdateTime(new Date());
                /*更新输入的信息*/
                disablerUserMapper.updateByPrimaryKey(disablerUser);
            } catch (Exception e) {
                return new DisablerExecution(DisablerStateEnum.FAIL);
            }
            return new DisablerExecution(DisablerStateEnum.SUCCESS, disablerUser);
        }
    }


    /*增加图片*/
    private String addDisablerHeadImg(DisablerUser disablerUser,InputStream HeadImgInputStream,String fileName){
        /*获取头像图片目录的相对路径*/
        String dest = PathUtil.getDisablerHeadImgPath();
        String disablerHeadImgAddr = ImageUtil.ThumbnailsImg(HeadImgInputStream,fileName,dest);
        return disablerHeadImgAddr;
    }

    /*判断用户手机号是否重复*/
    private boolean checkPhone(DisablerUser disablerUser){
        List<String> listPhone = disablerUserMapper.getAllPhone();
        for (String phone:listPhone) {
            if (disablerUser.getPhoneNo().equals(phone)) {
                return true;
            }
        }
        return false;
    }

    /*判断用户名是否存在*/
    private boolean checkUsername(String username){
        List<String> listUsername = disablerUserMapper.getAllUsername();
        for (String username_1:listUsername) {
            if (username.equals(username_1)) {
                return true;
            }
        }
        return false;
    }

    /*判断用户名是否重复*/
    private boolean checkUsername(DisablerUser disablerUser){
        List<String> listUsername = disablerUserMapper.getAllUsername();
        for (String username:listUsername) {
            if (disablerUser.getUsername().equals(username)) {
                return true;
            }
        }
        return false;
    }
}

